package com.xuan.book.controller;


import com.xuan.book.constants.Constants;
import com.xuan.book.model.*;
import com.xuan.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;


    @RequestMapping("/addBook")
    public Result addBook(BookInfo bookInfo) {
        log.info("添加图书，bookInfo:{}", bookInfo);
        if(bookInfo == null || !StringUtils.hasLength(bookInfo.getBookName())
                            || !StringUtils.hasLength(bookInfo.getAuthor())
                            || bookInfo.getCount() < 0
                            || bookInfo.getPrice() == null
                            || !StringUtils.hasLength(bookInfo.getPublish())) {
            return Result.fail("参数错误");
        }

        try {
            bookService.addBook(bookInfo);
        } catch (Exception e) {
            return Result.fail("内部错误，请联系管理员");
        }
        return Result.success("");
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询列表信息，pageRequest{}", pageRequest);

        if(pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数非法");
        }

        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);

    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        if(bookId < 1) {
            log.info("非法图书id, bookId:" + bookId);
            return null;
        }

        log.info("查询图书信息, bookId：" + bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.error("更新图书 updateBook{}", bookInfo);
        // 当更新状态不为删除时，对应的属性值要合理
        if(bookInfo == null || bookInfo.getId() < 1 || (bookInfo.getStatus() != 0 && (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())))) {
            return Result.fail("id不合法");
        }
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result < 1) return Result.fail(false, "更新失败");
        } catch (Exception e) {
            log.error("更新图书失败, e:" , e);
            return Result.fail("内部发生异常");
        }
        return Result.success(true);
    }

    @RequestMapping("/batchDelete")
    public boolean batchDeleteBook(int[] ids) {
        if(ids != null) {
            return bookService.batchDeleteBook(ids);
        }
        return false;
    }
}
